package com.lzb.play.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.play.BaseAc;
import com.lzb.play.HomeActivity;
import com.lzb.play.R;
import com.lzb.play.bean.PianoKeyBean;
import com.lzb.play.databinding.ActivityPianoSettingBinding;
import com.lzb.play.dialog.SelBirdDialog;
import com.lzb.play.utils.AppSpUtils;
import com.lzb.play.view.PianoKeyboardView;

import java.util.ArrayList;
import java.util.List;

public class PianoSettingActivity extends BaseAc<ActivityPianoSettingBinding> implements View.OnClickListener {

    private List<PianoKeyBean> listShow = new ArrayList<>();
    private SelBirdDialog selBirdDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_piano_setting;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPianoSettingBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivSave.setOnClickListener(this);

        mDataBinding.pianoView.setSetting(true);

        mDataBinding.pianoView.setOnPianoKeyListener(new PianoKeyboardView.OnPianoKeyListener() {
            @Override
            public void onWhiteKeyPressed(int keyIndex) {
                showSelBirdDialog(keyIndex);
            }

            @Override
            public void onWhiteKeyReleased(int keyIndex) {

            }

            @Override
            public void onBlackKeyPressed(int keyIndex) {

            }

            @Override
            public void onBlackKeyReleased(int keyIndex) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivSave) {//保存
            AppSpUtils.savePianoKeyList(listShow);
            ToastUtils.showShort(getString(R.string.save_suc));
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        List<PianoKeyBean> listSp = AppSpUtils.getPianoKeyList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
        } else {
            listSp = new ArrayList<>();
            listSp.add(new PianoKeyBean(0, 0, false));
            listSp.add(new PianoKeyBean(1, 0, false));
            listSp.add(new PianoKeyBean(2, 0, false));
            listSp.add(new PianoKeyBean(3, 0, false));
            listSp.add(new PianoKeyBean(4, 0, false));
            listSp.add(new PianoKeyBean(5, 0, false));
            listSp.add(new PianoKeyBean(6, 0, false));
            AppSpUtils.savePianoKeyList(listSp);
            listShow.addAll(listSp);
        }
        for (int i = 0; i < listShow.size(); i++) {
            mDataBinding.pianoView.setWhiteKeyPressed(i, listShow.get(i).isSetting());
        }
    }

    private void showSelBirdDialog(int position) {
        if (selBirdDialog == null) selBirdDialog = new SelBirdDialog(mContext);
        selBirdDialog.setListener(new SelBirdDialog.Ilistener() {
            @Override
            public void clickConfirm(Integer id) {
                listShow.get(position).setRawId(id);
                listShow.get(position).setSetting(true);
                mDataBinding.pianoView.setWhiteKeyPressed(position, true);
            }
        });
        selBirdDialog.show();
    }

}